//
//  PartialTableViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/25.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class PartialTableViewController: PurchaseTableViewController {
    
    var partial = true

    var partialCount = 10
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        let footer = UIView()
//
//        footer.backgroundColor = UIColor.white
//
//        footer.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: self.view.width, height: 30))
        
//        let lookRemaining = UIButton.init(type: .system)
//
//        lookRemaining.setTitle("查看剩余基本信息", for: .normal)
//
//        lookRemaining.size = CGSize(width: 150, height: 30)
//
//        lookRemaining.setTitleColor(UIColor.mainColor, for: .normal)
//
//        lookRemaining.center = footer.center
//
//        lookRemaining.addTarget(self, action: #selector(change(btn:)), for: .touchUpInside)
//
//        footer.addSubview(lookRemaining)
//
//        self.tableView.tableFooterView = footer
        
        self.change(btn: UIButton())
    }
    
    @objc func change(btn: UIButton) -> Void {
        partial = !partial
        if partial == true {
            
            self.tableView.beginUpdates()
            
            let indexs = (partialCount ..< self.source.count).map { (index) -> IndexPath in
                return IndexPath.init(row: index, section: 0)
            }
            
            self.tableView.deleteRows(at: indexs, with: .automatic)
            
            let sections = IndexSet.init(integer: 1)
            
            self.tableView.deleteSections(sections, with: .automatic)
            
            self.tableView.endUpdates()
            
            btn.setTitle("查看剩余基本信息", for: .normal)
        } else {
            
            self.tableView.beginUpdates()
            
            let indexs = (partialCount ..< self.source.count).map { (index) -> IndexPath in
                return IndexPath.init(row: index, section: 0)
            }
            
            self.tableView.insertRows(at: indexs, with: .automatic)
            
            let sections = IndexSet.init(integer: 1)
            
            self.tableView.insertSections(sections, with: .automatic)
            
            self.tableView.endUpdates()
            
            btn.setTitle("收起", for: .normal)
        }
        
        self.tableView.reloadData()
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return partial ? 1 : 2
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        switch section {
        case 0:
            return partial ? ( self.source.count > partialCount ? partialCount : 0 ) : self.source.count
        default:
            return self.documents.count
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
